<?php namespace Admin\Controller;
use Admin\Controller\Controller;
class GoodslistsController extends CommonController{
	protected $glmodel;
	protected $gamodel;
//	自动验证登陆
public function __init(){
	parent::__init();
	$this->glmodel=new \Common\Model\Goodslists;
	
}
//	添加显示
public function index(){
	
	//		获得商品gid
	$gid=Q('get.gid',0,'intval');
	//	获得组合id
	//	获得商品类型id
	$gamodel=new \Common\Model\GoodsAttribute;
	$gadata=$gamodel->join('typeattribute','taid','=','typeattribute_taid')->where("goods_gid={$gid} and taclass=1")->get();
	foreach($gadata as $k=>$v){
		$gadata[$k]['tavalue']=explode(',', $v['tavalue']);
	}
//	sp($gadata);

	//重组数组
	$title=array();
	foreach($gadata as $tg=>$tv){
		$title[$tv['taname']][]=$tv;
	}
	//	分配
	View::with('title',$title);
//	添加
//		sp($title);die; 
	if(IS_POST){
		$data=$_POST;
		$data['combine']=implode(',',$data['combine']);
		//		查找数据库
		$lookdata=$this->glmodel->where('goods_gid',$gid)->pluck('combine');
		if($data['combine']==$lookdata){
			View::success('规格已存在，不能添加',U('Goodslists/index',array('gid'=>$_GET['gid'])));
		}
//		sp($lookdata);die;

//		sp($data);die;
		//		执行添加
		if(!$this->glmodel->store($data)){
			View::error($this->glmodel->getError());
		}
		
		View::success('添加成功',U('Goodslists/index',array('gid'=>$_GET['gid'])));
		
	}
//	显示
//获得数据
//获得类型
	$gldata=$this->glmodel->where('goods_gid',$gid)->get();
//	sp($title);
//	sp($gadata);die;
	//	关联类型属性和商品属性
//	1改变数组样式
	foreach($gldata as $k=>$v){
		
		$gldata[$k]['combine']=explode(',', $v['combine']);
	}
//	遍历数组给$v添加一个gavlue字段
	foreach($gldata as $k=>$v){
		foreach($v['combine'] as $vk=>$vv){
			$gldata[$k]['gavalue'][]=$gamodel->where('gaid',$vv)->pluck('gavalue');
		}
	}
//	sp($gldata);die;
	//	分配
	View::with('gldata',$gldata);
	View::make();
}


//	删除
	public function del(){
//		获得glid
	$glid=Q('get.glid',0,'intval');
	$gid=Q('get.gid',0,'intval');
//	sp($glid);
//	sp($gid);die;
		if(!$this->glmodel->del($glid)){
			View::error($this->glmodel->getError());
		}
		View::success('删除成功',U('Goodslists/index',array('gid'=>$gid)));
	}
	
//	修改
public function edit(){
//	获得旧数据
$glid=Q('get.glid',0,'intval');
$gid=Q('get.gid',0,'intval');
//sp($glid);
$olddata=$this->glmodel->where('glid',$glid)->find();
//sp($olddata);

//获得商品类型属性
$goodsattribute=new \Common\Model\GoodsAttribute;
//修改数组样式
	$olddata['combine']=explode(',', $olddata['combine']);
//	sp($olddata);
	foreach($olddata['combine'] as $k=>$v){
		$olddata['gavalue'][]=$goodsattribute->where('gaid',$v)->pluck('gavalue');
	}
	
//sp($olddata['combine']);
View::with('olddata',$olddata);

//添加循环
//关联类型属性表
$gadata=$goodsattribute->join('typeattribute','typeattribute_taid','=','taid')->where("goods_gid={$gid} and taclass=1")->get();
//sp($gadata);die;
foreach($gadata as $k=>$v){
	$gadata[$k]['tavalue']=explode(',',$v['tavalue']);
}

//sp($gadata);die;
//重组数组
$againdata=array();

foreach($gadata as $k=>$v){
	$againdata[$v['taname']][]=$v;
}
View::with('againdata',$againdata);
//sp($againdata);die;


//修改
if(IS_POST){
//查找数据库
$data=$_POST;
		$data['combine']=implode(',',$data['combine']);
		
		
		//		查找数据库
		$lookdata=$this->glmodel->where("goods_gid=$gid and glid!=$glid")->field('combine')->get();
//		sp($lookdata);die;
		//		数据库数组
		$basedata=array();
		foreach($lookdata as $v){
			$basedata[]=$v['combine'];
		}
//		sp($v['combine']);die;/
//		sp($basedata);die;
		
		if(in_array($data['combine'], $basedata)){
			View::success('规格已存在，不能添加',U('Goodslists/index',array('gid'=>$_GET['gid'])));
		}
//执行添加
if(!$this->glmodel->edit($glid,$data)) {
//	echo 123;
	View::error($this->glmodel->getError());
	}
//echo 123;
View::success('修改成功',U('index',array('gid'=>$gid)));
//sp($data);die;
}


	View::make();
}
	
	
	
	
	
	
}
